package com.lineage.server.model;

import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.datatables.lock.PetReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.templates.L1Pet;
import com.lineage.server.world.World;
import java.util.Timer;
import java.util.TimerTask;

public class L1PetMatch {
  public static final int STATUS_NONE = 0;
  
  public static final int STATUS_READY1 = 1;
  
  public static final int STATUS_READY2 = 2;
  
  public static final int STATUS_PLAYING = 3;
  
  public static final int MAX_PET_MATCH = 1;
  
  private static final short[] PET_MATCH_MAPID = new short[] { 5125, 5131, 5132, 5133, 5134 };
  
  private String[] _pc1Name = new String[1];
  
  private String[] _pc2Name = new String[1];
  
  private L1PetInstance[] _pet1 = new L1PetInstance[1];
  
  private L1PetInstance[] _pet2 = new L1PetInstance[1];
  
  private static L1PetMatch _instance;
  
  public static L1PetMatch getInstance() {
    if (_instance == null)
      _instance = new L1PetMatch(); 
    return _instance;
  }
  
  public int setPetMatchPc(int petMatchNo, L1PcInstance pc, L1PetInstance pet) {
    int status = getPetMatchStatus(petMatchNo);
    if (status == 0) {
      this._pc1Name[petMatchNo] = pc.getName();
      this._pet1[petMatchNo] = pet;
      return 1;
    } 
    if (status == 1) {
      this._pc2Name[petMatchNo] = pc.getName();
      this._pet2[petMatchNo] = pet;
      return 3;
    } 
    if (status == 2) {
      this._pc1Name[petMatchNo] = pc.getName();
      this._pet1[petMatchNo] = pet;
      return 3;
    } 
    return 0;
  }
  
  private synchronized int getPetMatchStatus(int petMatchNo) {
    L1PcInstance pc1 = null;
    if (this._pc1Name[petMatchNo] != null)
      pc1 = World.get().getPlayer(this._pc1Name[petMatchNo]); 
    L1PcInstance pc2 = null;
    if (this._pc2Name[petMatchNo] != null)
      pc2 = World.get().getPlayer(this._pc2Name[petMatchNo]); 
    if (pc1 == null && pc2 == null)
      return 0; 
    if (pc1 == null && pc2 != null) {
      if (pc2.getMapId() == PET_MATCH_MAPID[petMatchNo])
        return 2; 
      this._pc2Name[petMatchNo] = null;
      this._pet2[petMatchNo] = null;
      return 0;
    } 
    if (pc1 != null && pc2 == null) {
      if (pc1.getMapId() == PET_MATCH_MAPID[petMatchNo])
        return 1; 
      this._pc1Name[petMatchNo] = null;
      this._pet1[petMatchNo] = null;
      return 0;
    } 
    if (pc1.getMapId() == PET_MATCH_MAPID[petMatchNo] && 
      pc2.getMapId() == PET_MATCH_MAPID[petMatchNo])
      return 3; 
    if (pc1.getMapId() == PET_MATCH_MAPID[petMatchNo]) {
      this._pc2Name[petMatchNo] = null;
      this._pet2[petMatchNo] = null;
      return 1;
    } 
    if (pc2.getMapId() == PET_MATCH_MAPID[petMatchNo]) {
      this._pc1Name[petMatchNo] = null;
      this._pet1[petMatchNo] = null;
      return 2;
    } 
    return 0;
  }
  
  private int decidePetMatchNo() {
    int i = 0;
    while (i < 1) {
      int status = getPetMatchStatus(i);
      if (status == 1 || status == 2)
        return i; 
      i++;
    } 
    i = 0;
    while (i < 1) {
      int status = getPetMatchStatus(i);
      if (status == 0)
        return i; 
      i++;
    } 
    return -1;
  }
  
  public synchronized boolean enterPetMatch(L1PcInstance pc, int amuletId) {
    int petMatchNo = decidePetMatchNo();
    if (petMatchNo == -1)
      return false; 
    L1PetInstance pet = withdrawPet(pc, amuletId);
    L1Teleport.teleport(pc, 32799, 32868, PET_MATCH_MAPID[petMatchNo], 0, true);
    L1PetMatchReadyTimer timer = new L1PetMatchReadyTimer(petMatchNo, pc, pet);
    timer.begin();
    return true;
  }
  
  private L1PetInstance withdrawPet(L1PcInstance pc, int amuletId) {
    L1Pet l1pet = PetReading.get().getTemplate(amuletId);
    if (l1pet == null)
      return null; 
    L1Npc npcTemp = NpcTable.get().getTemplate(l1pet.get_npcid());
    L1PetInstance pet = new L1PetInstance(npcTemp, pc, l1pet);
    pet.setPetcost(6);
    return pet;
  }
  
  public void startPetMatch(int petMatchNo) {
    this._pet1[petMatchNo].setCurrentPetStatus(1);
    this._pet1[petMatchNo].setTarget((L1Character)this._pet2[petMatchNo]);
    this._pet2[petMatchNo].setCurrentPetStatus(1);
    this._pet2[petMatchNo].setTarget((L1Character)this._pet1[petMatchNo]);
    L1PetMatchTimer timer = new L1PetMatchTimer(this._pet1[petMatchNo], this._pet2[petMatchNo], petMatchNo);
    timer.begin();
  }
  
  public void endPetMatch(int petMatchNo, int winNo) {
    L1PcInstance pc1 = World.get().getPlayer(this._pc1Name[petMatchNo]);
    L1PcInstance pc2 = World.get().getPlayer(this._pc2Name[petMatchNo]);
    if (winNo == 1) {
      giveMedal(pc1, petMatchNo, true);
      giveMedal(pc2, petMatchNo, false);
    } else if (winNo == 2) {
      giveMedal(pc1, petMatchNo, false);
      giveMedal(pc2, petMatchNo, true);
    } else if (winNo == 3) {
      giveMedal(pc1, petMatchNo, false);
      giveMedal(pc2, petMatchNo, false);
    } 
    qiutPetMatch(petMatchNo);
  }
  
  private void giveMedal(L1PcInstance pc, int petMatchNo, boolean isWin) {
    if (pc == null)
      return; 
    if (pc.getMapId() != PET_MATCH_MAPID[petMatchNo])
      return; 
    if (isWin) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(1166, pc.getName()));
      L1ItemInstance item = ItemTable.get().createItem(41309);
      long count = 3L;
      if (item != null && pc.getInventory().checkAddItem(item, 3L) == 0) {
        item.setCount(3L);
        pc.getInventory().storeItem(item);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
      } 
    } else {
      L1ItemInstance item = ItemTable.get().createItem(41309);
      long count = 1L;
      if (item != null && pc.getInventory().checkAddItem(item, 1L) == 0) {
        item.setCount(1L);
        pc.getInventory().storeItem(item);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
      } 
    } 
  }
  
  private void qiutPetMatch(int petMatchNo) {
    L1PcInstance pc1 = World.get().getPlayer(this._pc1Name[petMatchNo]);
    if (pc1 != null && pc1.getMapId() == PET_MATCH_MAPID[petMatchNo]) {
      Object[] array;
      int i = (array = pc1.getPetList().values().toArray()).length;
      int j = 0;
      while (j < i) {
        Object object = array[j];
        if (object instanceof L1PetInstance) {
          L1PetInstance pet = (L1PetInstance)object;
          pet.dropItem();
          pc1.getPetList().remove(Integer.valueOf(pet.getId()));
          pet.deleteMe();
        } 
        j++;
      } 
      L1Teleport.teleport(pc1, 32630, 32744, (short)4, 4, true);
    } 
    this._pc1Name[petMatchNo] = null;
    this._pet1[petMatchNo] = null;
    L1PcInstance pc2 = World.get().getPlayer(this._pc2Name[petMatchNo]);
    if (pc2 != null && pc2.getMapId() == PET_MATCH_MAPID[petMatchNo]) {
      Object[] array2;
      int length = (array2 = pc2.getPetList().values().toArray()).length;
      int i = 0;
      while (i < length) {
        Object object2 = array2[i];
        if (object2 instanceof L1PetInstance) {
          L1PetInstance pet = (L1PetInstance)object2;
          pet.dropItem();
          pc2.getPetList().remove(Integer.valueOf(pet.getId()));
          pet.deleteMe();
        } 
        i++;
      } 
      L1Teleport.teleport(pc2, 32630, 32744, (short)4, 4, true);
    } 
    this._pc2Name[petMatchNo] = null;
    this._pet2[petMatchNo] = null;
  }
  
  public class L1PetMatchReadyTimer extends TimerTask {
    private final int _petMatchNo;
    
    private final L1PcInstance _pc;
    
    private final L1PetInstance _pet;
    
    public L1PetMatchReadyTimer(int petMatchNo, L1PcInstance pc, L1PetInstance pet) {
      this._petMatchNo = petMatchNo;
      this._pc = pc;
      this._pet = pet;
    }
    
    public void begin() {
      Timer timer = new Timer();
      timer.schedule(this, 3000L);
    }
    
    public void run() {
      try {
        while (true) {
          Thread.sleep(1000L);
          if (this._pc == null || this._pet == null) {
            cancel();
            return;
          } 
          if (!this._pc.isTeleport()) {
            if (L1PetMatch.getInstance().setPetMatchPc(this._petMatchNo, this._pc, this._pet) == 3)
              L1PetMatch.getInstance().startPetMatch(this._petMatchNo); 
            cancel();
            return;
          } 
        } 
      } catch (Throwable t) {
        return;
      } 
    }
  }
  
  public class L1PetMatchTimer extends TimerTask {
    private final L1PetInstance _pet1;
    
    private final L1PetInstance _pet2;
    
    private final int _petMatchNo;
    
    private int _counter;
    
    public L1PetMatchTimer(L1PetInstance pet1, L1PetInstance pet2, int petMatchNo) {
      this._counter = 0;
      this._pet1 = pet1;
      this._pet2 = pet2;
      this._petMatchNo = petMatchNo;
    }
    
    public void begin() {
      Timer timer = new Timer();
      timer.schedule(this, 0L);
    }
    
    public void run() {
      try {
        while (true) {
          Thread.sleep(3000L);
          this._counter++;
          if (this._pet1 == null || this._pet2 == null) {
            cancel();
            return;
          } 
          if (this._pet1.isDead() || this._pet2.isDead()) {
            int winner = 0;
            if (!this._pet1.isDead() && this._pet2.isDead()) {
              winner = 1;
            } else if (this._pet1.isDead() && !this._pet2.isDead()) {
              winner = 2;
            } else {
              winner = 3;
            } 
            L1PetMatch.getInstance().endPetMatch(this._petMatchNo, winner);
            cancel();
            return;
          } 
          if (this._counter == 100) {
            L1PetMatch.getInstance().endPetMatch(this._petMatchNo, 3);
            cancel();
            return;
          } 
        } 
      } catch (Throwable t) {
        return;
      } 
    }
  }
}
